/**
 * User: yuhao
 * Date: 15-8-24
 * Time: 下午3:05
 * 图层配置的操作方法
 */

define(
    [
        "require",
        "dojo/_base/declare",
        "dojo/_base/lang",
        "dojo/_base/array",
        "esri/geometry/Point",
        "esri/SpatialReference",
        "../util/utils",
        "../../app/appConfig"
    ],
    function (require, declare, lang, array, Point, SpatialReference, utils, appConfig) {

        return {

            /**
             * 创建默认的图层配置(eds数据的图层配置)
             * @param layerConfigId
             * @param layerConfigName
             * @param geometryType
             * @return {Object}
             */
            createDefaultLayerConfig:function (layerConfigId, layerConfigName, geometryType) {
                var cfg = {
                    basic:{
                        label:{
                            enable:true,
                            show:false
                        },
                        opacity:0
                    },
                    visible:false
                };

                if (geometryType === "point") {
                    cfg.style = {
                        "symbol":appConfig.defaults.symbol["marker"],
                        "type":"marker",
                        "geometry":"point"
                    };
                }
                else if (geometryType === "polygon") {
                    cfg.style = {
                        "symbol":appConfig.defaults.symbol["fill"],
                        "type":"fill",
                        "geometry":"polygon"
                    };
                }

                return {
                    id:layerConfigId,
                    layerName:layerConfigName,
                    cfg:cfg,
                    dataSourceId:dataConfig.dataSourceId
                };
            },

            /**
             * 根据图层渲染器获取初始样式配置
             * @param renderer
             */
            getStyleConfig:function (renderer) {
                if (!renderer) {
                    return null;
                }
                var declass = renderer.declaredClass;

                switch (declass) {
                    case "esri.renderer.SimpleRenderer":
                        return this.getSimpleStyleConfig(renderer);
                    case "esri.renderer.UniqueValueRenderer":
                        return this.getUniqueValueStyleConfig(renderer);
                    default:
                        return null;
                }
            },

            /**
             * 获取简单渲染器样式
             * @param renderer
             */
            getSimpleStyleConfig:function (renderer) {
                var sym = renderer.symbol,
                    c = sym.color,
                    oc = sym.outline.color;

                //TODO:这里应使用rgba，用hex会忽略掉透明度
                var symbol = {
                    border:sym.outline.width,
                    color:c.toRgba(),
                    borderColor:oc.toRgba()
                };
                return { type:"fill", symbol:symbol };
            },

            /**
             * 获取唯一值渲染器样式
             * @param renderer
             */
            getUniqueValueStyleConfig:function (renderer) {
                var infos = renderer.infos,
                    symbol = {
                        field:renderer.attributeField,
                        values:[],
                        colors:[]
                    };

                array.forEach(infos, function (f) {
                    symbol.values.push(f.value);
                    //TODO:这里应使用rgba，用hex会忽略掉透明度
                    symbol.colors.push(f.symbol.color.toHex());
                });
                return {
                    type:"types",
                    symbol:symbol
                };
            }
        };
    });